package com.linzp.bishe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linzp.bishe.common.Constants;
import com.linzp.bishe.common.RedisCache;
import com.linzp.bishe.common.TokenContent;
import com.linzp.bishe.controller.exception.ServiceException;
import com.linzp.bishe.entity.Accounts;
import com.linzp.bishe.entity.Users;
import com.linzp.bishe.mapper.AccountsMapper;
import com.linzp.bishe.mapper.UsersMapper;
import com.linzp.bishe.model.AccountSearch;
import com.linzp.bishe.model.AccountUser;
import com.linzp.bishe.model.LoginUser;
import com.linzp.bishe.model.SimpleAccountUser;
import com.linzp.bishe.service.AccountsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 林泽鹏
 * @since 2021-11-01
 */
@Service
public class AccountsServiceImpl extends ServiceImpl<AccountsMapper, Accounts> implements AccountsService {

	/**
	 * @Description 令牌 token
	 */
	@Autowired
	private TokenContent tokenContent;

	@Autowired
	private RedisCache redisCache;

	@Autowired
	private AccountsMapper accountsMapper;

	@Autowired
	private UsersMapper usersMapper;


	@Override
	public String login(String account, String password, String code, String uuid) {
		validateCaptcha(account, code, uuid);
		QueryWrapper<Accounts> wrapper = new QueryWrapper<>();
		wrapper.eq("accounts",account);
		Accounts one = getOne(wrapper);
		if (one == null){
			throw new ServiceException("用户不存在/账号错误",null);
		}
		else if (password == null |  !Objects.equals(one.getPasswords(),password)){
			throw new ServiceException("账号/密码错误",null);
		}
		LoginUser user = new LoginUser(one);
		String token = tokenContent.createToken(user);
		tokenContent.setLoginUser(user);
		return token;
	}

	@Override
	public Accounts checkAccountStatus(HttpServletRequest httpServletRequest) {
		Accounts account = (Accounts) httpServletRequest.getAttribute("account");
		if(account == null) {
			 throw new ServiceException("登录失效请重新登录","401");
		}
		return account;
	}

	@Override
	public boolean checkAccountIsAdmin(HttpServletRequest httpServletRequest) {
		Accounts account = (Accounts) httpServletRequest.getAttribute("account");
		if(account == null | !account.getRoles().equals("管理员")) {
			throw new ServiceException("没有管理员权限请重新登录","401");
		}
		return true;
	}


	/**
	 * 校验验证码
	 *
	 * @param account 用户名
	 * @param code 验证码
	 * @param uuid 唯一标识
	 * @return 结果
	 */
	public void validateCaptcha(String account, String code, String uuid)
	{
		String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
		String captcha = redisCache.getCacheObject(verifyKey);
		redisCache.deleteObject(verifyKey);
		if (captcha == null)
		{
			throw new ServiceException("验证码已失效",null);
		}
		if (!code.equalsIgnoreCase(captcha))
		{
			throw new ServiceException("验证码错误",null);
		}
	}


	@Override
	public List<AccountUser> getAccountsInformation(int currentPage, int pageSize, AccountSearch search) {
		int num = currentPage * pageSize;
		if (search.getName()!=null & !search.getName().isEmpty()){
			search.setName('%'+search.getName()+'%');
		}
		if (search.getRoles()!=null & !search.getRoles().isEmpty()){
			search.setRoles('%'+search.getRoles()+'%');
		}
		List<AccountUser> accountUser = accountsMapper.getAccountUser(num - pageSize, pageSize,search);
		return accountUser;
	}

	/**
	 * @Description 增加账号、用户信息
	 * @Param [accountUser]
	 * @return void
	 */
	@Override
	public void addAccountUser(AccountUser accountUser) {
		Accounts accounts = new Accounts();
		accounts.setAccounts(accountUser.getAccounts());
		accounts.setPasswords(accountUser.getPasswords());
		accounts.setRoles(accountUser.getRoles());

		QueryWrapper<Accounts> wrapper =new QueryWrapper<>();
		wrapper.eq("accounts",accounts.getAccounts());
		Accounts one = this.getOne(wrapper);
		if(one != null){
			throw new ServiceException("账号已存在,如需添加请联系管理员",null);
		}

		accountsMapper.insert(accounts);
		Users users = accountUser.getUsers();
		users.setAccountId(accounts.getId());
		usersMapper.insert(users);
	}

	/**
	 * @Description 返回所有人员信息，用于参与人员
	 * @Param [search]
	 * @return java.util.List<com.linzp.bishe.model.SimpleAccountUser>
	 */
	@Override
	public List<SimpleAccountUser> getAllSimpleAccountUser(Integer currentPage, Integer pageSize,AccountSearch search) {
		int num = currentPage * pageSize;
		if (search.getName()!=null & !search.getName().isEmpty()){
			search.setName('%'+search.getName()+'%');
		}
		List<SimpleAccountUser> user = accountsMapper.getAllSimpleAccountUser(num - pageSize, pageSize, search);
		return user;
	}

	@Override
	public Integer getAllSimpleAccountUserCount(Integer currentPage, Integer pageSize, AccountSearch search) {
		return accountsMapper.getAllSimpleAccountUserCount(currentPage * pageSize - pageSize, pageSize, search);
	}

	/**
	 * @Description 返回一位参与人员
	 * @Param [id]
	 * @return com.linzp.bishe.model.SimpleAccountUser
	 */
	@Override
	public SimpleAccountUser getSimpleAccountUser(Integer id) {
		return accountsMapper.getSimpleAccountUser(id);
	}

}
